package model;

import controller.exceptions.InvalidVersionException;
import controller.exceptions.ItemAlreadyRemovedException;
import filesystem.Assistants;
import filesystem.exceptions.InvalidModelItemException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/**
 * The track model class
 */
public class TrackModel extends BaseModel implements Model {

    public TrackModel() {
    }
    
    /**
     * Select items from the model by parametr
     *
     * @param parametr parametr of the search
     * @return list of the found objects
     */
    public ArrayList selectItems() throws IOException {
        getItems().clear();
        super.setItems(Assistants.tFA.getItems());
        return super.getItems();
    }
    
    public ArrayList selectItems(ModelItem parametr) throws IOException {
        getItems().clear();
        HashMap map = parametr.itemToMap();
        super.setItems(Assistants.tFA.getItems(map));
        return super.getItems();
    }

    /**
     * Add object to the model
     *
     * @param item new object
     */
    public void addItem(ModelItem item) throws InvalidModelItemException, IOException {
        super.getItems().add(item);
        Assistants.getFileAssistant(((Track) item).getAlbum()).addItem(((Track) item).getAlbum());
        Assistants.getFileAssistant(((Track) item).getArtist()).addItem(((Track) item).getArtist());
        Assistants.getFileAssistant(((Track) item).getGenre()).addItem(((Track) item).getGenre());
        Assistants.getFileAssistant(item).addItem(item);
    }

    /**
     * Edit object in the model
     *
     * @param item changing object
     */
    public void setItem(ModelItem item) throws InvalidModelItemException,
            IOException, InvalidVersionException, ItemAlreadyRemovedException {
        //ищем объект в загруженной модели
        int i = 0;
        Iterator it = getItems().iterator();
        ModelItem buf = null;
        while (it.hasNext()) {
            buf = (Track) it.next();
            if (item.getId() == buf.getId()) {
                if (item.getVersion() != buf.getVersion()) {
                    throw new InvalidVersionException();
                }
                item.incrementVersion();
                getItems().set(i, item);
                break;
            }
            i++;
            if (!it.hasNext()) {
                throw new ItemAlreadyRemovedException();
            }
        }
        //сохраняем изменения
        Assistants.getFileAssistant(((Track) item).getAlbum()).addItem(((Track) item).getAlbum());
        Assistants.getFileAssistant(((Track) item).getArtist()).addItem(((Track) item).getArtist());
        Assistants.getFileAssistant(((Track) item).getGenre()).addItem(((Track) item).getGenre());
        Assistants.getFileAssistant(item).setItem(item);
        Assistants.getFileAssistant(item).resetLoaded();
    }

    /**
     * Remove object from the model
     *
     * @param item removing object
     */
    public void removeItem(ModelItem item) throws InvalidModelItemException, IOException {
        //проверяем, есть ли в треке нулевые указатели
        if (((Track) item).getAlbum() == null) {
            Album album = new Album("deleting");
            ((Track) item).setAlbum(album);
        }
        if (((Track) item).getArtist() == null) {
            Artist artist = new Artist("deleting");
            ((Track) item).setArtist(artist);
        }
        if (((Track) item).getGenre() == null) {
            Genre genre = new Genre("deleting");
            ((Track) item).setGenre(genre);
        }
        getItems().remove(item);
        Assistants.getFileAssistant(item).removeItem(item.getId());
        Assistants.tFA.resetLoaded();
    }
}
